package com.azzcs.catalina;

import com.azzcs.catalina.pipeline.Pipeline;
import com.azzcs.catalina.pipeline.StandardPipeline;
import com.azzcs.lifecycle.LifecycleBase;

import java.util.*;

/**
 * @Author: wzg
 * @Date: 2021/1/9 上午8:58
 */
public abstract class ContainerBase extends LifecycleBase implements Container {

    protected Pipeline pipeline = new StandardPipeline(this);

    protected Container parent;
    protected String name;

    protected Map<String,Container> childMap = new HashMap<>();
    protected List<Container> childList = new ArrayList<>();

    @Override
    public Pipeline getPipeline() {
        return pipeline;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public Container getParent() {
        return parent;
    }

    @Override
    public void setParent(Container container) {
        this.parent = container;
    }

    @Override
    public void addChild(Container child) {
        this.childMap.put(child.getName(),child);
        this.childList.add(child);
    }

    @Override
    public Container findChild(String name) {
        return this.childMap.get(name);
    }

    @Override
    public List<Container> findChildren() {
        return this.childList;
    }

    @Override
    public void removeChild(Container child) {
        this.childList.remove(child);
        this.childMap.remove(child.getName());
    }

    @Override
    protected void initInternal() {

    }

    @Override
    protected void startInternal() {

    }
}
